<template>
  <div class="shop-cart">
    <!-- 顶部导航 -->
    <nav-bar class="cart-nav">
      <div slot="center">
        购物街(<span>{{ length }}</span
        >)
      </div>
    </nav-bar>

    <!-- 重构滚动 -->
    <scroll class="content" ref="scroll">
      <!-- 购物车列表 -->
      <cart-list />
    </scroll>

    <!-- 结算面板 -->
    <cart-bottom-bar />
  </div>
</template>

<script>
//子组件
import CartList from './childComps/CartList'
import CartBottomBar from './childComps/CartBottomBar'

//公用组件
import NavBar from 'components/common/navbar/NavBar'
import Scroll from 'components/common/scroll/Scroll'

//工具组件
import { mapGetters } from 'vuex'
import { itemImglistener } from 'common/mixin'

export default {
  name: 'shopCart',
  components: {
    NavBar,
    CartList,
    CartBottomBar,

    Scroll
  },
  data() {
    return {}
  },
  methods: {},
  computed: {
    // 通过辅助函数,可以直接使用getters中的属性，按需引入
    // 语法一
    // 直接引入
    // ...mapGetters(['cartLength', 'cartList'])

    // 语法二
    // 别名引入
    ...mapGetters({
      length: 'cartLength'
    })
  },
  mixins: [itemImglistener],
  //路由重新活跃时,跳转至保存状态
  activated() {
    this.$refs.scroll.refresh()
    this.$refs.scroll.scrollTo(0, this.saveY, 0)
  },
  //路由失去活跃时,记录当前状态
  deactivated() {
    this.saveY = this.$refs.scroll.getScrollY()
    //停止监听这个事件,还要停止对应的函数
    this.$bus.$off('imgLoad', this.itemImglistener)
  }
}
</script>

<style type="text/css" scoped>
.shop-cart {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.cart-nav {
  font-size: 18px;
  letter-spacing: 2px;
  color: #fff;
  background-color: var(--color-tint);
}
.content {
  flex: 1;
  overflow: hidden;
}
</style>
